package com.wave2.shadow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.wave2.shadow.persistence.EventsList;
import com.wave2.shadow.misc.Constants;

@Controller("eventsController")
@RequestMapping("events.htm")
public class EventsController {

	private static final String WS_URI_PREFIX = "http://localhost:8080/MavenJGA/api/event/";	//TODO: this should be injected in?
	public static final String WS_URI_BYNAME = WS_URI_PREFIX + "name/";							//TODO: this should be injected in?
	
	private static final String MY_VIEW_NAME = "showevents";									//TODO: this should be injected in.
	
	@Autowired
	RestTemplate restTemplate;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView showPage(ModelMap model) {
			
		EventsList events = restTemplate.getForObject(
				WS_URI_PREFIX, 
				EventsList.class);

		model.addAttribute(Constants.ATTR_EVENTLIST, events.getEvents());
		model.addAttribute(Constants.ATTR_WSURI_NAMEFILTER, WS_URI_BYNAME);
		
		ModelAndView mv = new ModelAndView(MY_VIEW_NAME, model); 
		
		return mv;
	}

}
